package com.cfx.exercise_39;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class PDFUtil {

	public static void main(String[] args) throws DocumentException, IOException {
		
		Document doc = new Document();
		//设置页面
				Rectangle rec = new Rectangle(PageSize.A4);
				doc.setPageSize(rec);
				doc.setMargins(50, 50, 50, 40);
				//设置字体
				BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
				Font font1 = new Font(baseFont,15,Font.BOLD);
				Font font2 = new Font(baseFont,11,Font.NORMAL);
				Font font3 = new Font(baseFont,10,Font.NORMAL);
				try {
					PdfWriter pw = PdfWriter.getInstance(doc, new FileOutputStream("pdf/example.pdf"));
					
					//文档属性
					doc.addTitle("Title@sample"); // 标题 
					doc.addAuthor("Author@zhangsan");// 作者 
					doc.addSubject("Subject@iText sample");// 主题 
					doc.addKeywords("Keywords@iText");// 关键字 
					doc.addCreator("Creator@iText");// 创建者 
					
					doc.open();
					
					Paragraph title = new Paragraph("黑河市公安局印章准刻通知书",font1);
					title.setAlignment(Element.ALIGN_CENTER);//设置居中
					
					Paragraph number = new Paragraph("批单编号：\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t审批日期：",font2);
					//number.setIndentationLeft(30f);//设置左距
					number.setLeading(50f);
					PdfPTable tableAll = new PdfPTable(1);
					tableAll.setWidthPercentage(100);
				
					PdfPTable table = new PdfPTable(4);						
					table.setWidthPercentage(100);					
					float[] widths = {3f,47f,9f,9f};
					table.setWidths(widths);
					PdfPCell cell = new PdfPCell(new Paragraph("申刻单位：",font2));
					cell.setFixedHeight(20f);
					cell.setColspan(4);
					PdfPCell cell1 = new PdfPCell(new Paragraph("法定代表人：\t\t\t\t\t\t\t\t\t经办人：\t\t\t\t\t\t\t\t\t联系电话：",font2));
					cell1.setColspan(4);
					cell1.setFixedHeight(20f);
					PdfPCell cell2 = new PdfPCell(new Paragraph("\n\n\n\n\n\n批\n\n准\n\n刻\n\n制\n\n印\n\n章",font2));					
					cell2.setRowspan(2);
					cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
					PdfPCell cell3 = new PdfPCell(new Paragraph("印章全称",font2));
					cell3.setFixedHeight(20f);
					cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
					PdfPCell cell4 = new PdfPCell(new Paragraph("印章类型",font2));
					cell4.setFixedHeight(20f);
					cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
					PdfPCell cell5 = new PdfPCell(new Paragraph("印章规格",font2));
					cell5.setFixedHeight(20f);
					cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
					PdfPCell cell6 = new PdfPCell();					
					PdfPCell cell7 = new PdfPCell();
					cell7.setColspan(3);
					cell7.setFixedHeight(250f);
					PdfPCell cell8 = new PdfPCell(new Paragraph("经办人签名：\t\t\t\t\t\t\t\t\t\t\t\t\t\t审批单位（盖章有效）\n\n\n\n"
							+ "承接密码：\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t审批人签字\n\n",font2));
					cell8.setColspan(4);
					
					table.addCell(cell);
					table.addCell(cell1);
					table.addCell(cell2);
					table.addCell(cell3);
					table.addCell(cell4);
					table.addCell(cell5);
					//table.addCell(cell6);
					table.addCell(cell7);
					table.addCell(cell8);
					Paragraph text = new Paragraph("注意事项：\n1.\t批单打印后，需审批单位盖章有效，有效期为5个工作日，请在"
							+ "有效期内持有该通知书到黑河市有资质的公章刻制企业进行印章刻制，逾期须重新申请；\n"
							+ "2.\t公章刻制企业将印章信息（印章编码、章面内容、审批地点等公开信息及加密信息）导入印章章体内的防伪芯片"
							+ "，芯片的内容可以用专门读写设备读取",font3);
					//text.setIndentationLeft(30f);
					//text.setIndentationRight(30f);
					tableAll.addCell(table);
					PdfPTable t = new PdfPTable(3);
					float[] width = {10f,10f,10f};
					t.setWidths(width);
					t.setWidthPercentage(101);
					//t.setSpacingBefore(500);
					for (int i = 0; i < 3; i++) {
						Paragraph p1 = new Paragraph("企业名称：",font3);
						p1.setAlignment(0);
						PdfPCell c1 = new PdfPCell(p1);
						c1.setHorizontalAlignment(Element.ALIGN_LEFT);
						c1.setVerticalAlignment(Element.ALIGN_CENTER);
						c1.setBorder(0);
						PdfPCell c2 = new PdfPCell(new Paragraph("企业地址：",font3));
						c2.setBorder(0);
						PdfPCell c3 = new PdfPCell(new Paragraph("企业电话：",font3));
						c3.setBorder(0);
						t.addCell(c1);
						t.addCell(c2);
						t.addCell(c3);
					}
					doc.add(title);
					doc.add(number);
					
					doc.add(tableAll);
					doc.add(new Paragraph("\n"));
					doc.add(text);
					doc.add(new Paragraph("\n"));
					doc.add(t);
					doc.close();
					pw.close();
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (DocumentException e) {
					e.printStackTrace();
				}
			}
	}

